package org.spamforces.model;

import com.codeforces.commons.comparator.ComplexComparator;
import com.codeforces.commons.text.StringUtil;

import java.util.Comparator;

/**
 * @author Maxim Shipko (sladethe@gmail.com)
 *         Date: 09.12.14
 */
public class Recipient implements Comparable<Recipient> {
    private static final Comparator<Recipient> DEFAULT_COMPARATOR = new ComplexComparator<>(
            new Comparator<Recipient>() {
                @SuppressWarnings({"AccessingNonPublicFieldOfAnotherObject", "StringEquality"})
                @Override
                public int compare(Recipient recipientA, Recipient recipientB) {
                    if (recipientA.email == recipientB.email) {
                        return 0;
                    }

                    if (recipientA.email == null) {
                        return 1;
                    }

                    if (recipientB.email == null) {
                        return -1;
                    }

                    return StringUtil.compareStringsSmart(recipientA.email, recipientB.email);
                }
            },
            new Comparator<Recipient>() {
                @SuppressWarnings({"AccessingNonPublicFieldOfAnotherObject", "StringEquality"})
                @Override
                public int compare(Recipient recipientA, Recipient recipientB) {
                    if (recipientA.phone == recipientB.phone) {
                        return 0;
                    }

                    if (recipientA.phone == null) {
                        return 1;
                    }

                    if (recipientB.phone == null) {
                        return -1;
                    }

                    return StringUtil.compareStringsSmart(recipientA.phone, recipientB.phone);
                }
            }
    );

    private String email;
    private String phone;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @SuppressWarnings("NonFinalFieldReferenceInEquals")
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }

        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Recipient recipient = (Recipient) o;

        return StringUtil.equals(email, recipient.email)
                && StringUtil.equals(phone, recipient.phone);
    }

    @SuppressWarnings("NonFinalFieldReferencedInHashCode")
    @Override
    public int hashCode() {
        int result = email == null ? 0 : email.hashCode();
        result = 31 * result + (phone == null ? 0 : phone.hashCode());
        return result;
    }

    @Override
    public int compareTo(Recipient recipient) {
        return DEFAULT_COMPARATOR.compare(this, recipient);
    }

    @Override
    public String toString() {
        return StringUtil.toString(this, true);
    }
}
